import json

firstIndex = 0
subjectIndex = 0
chapterIndex = 0
categoryIndex = 0
questionIndex = 0


def first_fun():
    root_json = '[{"level":"1","id":"A1000001","title":"一级建造师"},{"level":"2","id":"A1000002","title":"二级建造师"}]'
    return json.loads(root_json)


def subject_fun():
    content = first_fun()
    yijian_subject_titles = ['经济', '管理', '法规', '市政', '建筑', '机电', '水利', '通信', '公路', '铁路']
    erjian_subject_titles = ['法规', '机电', '公路', '管理', '建筑', '水利', '市政']
    for index, element in enumerate(content):
        subject_titles = []
        children = []
        if index == 0:
            subject_titles = yijian_subject_titles
        elif index == 1:
            subject_titles = erjian_subject_titles
        for index2, subject_title in enumerate(subject_titles):
            global subjectIndex
            subjectIndex = subjectIndex + 1
            subject = {'id': f"B{subjectIndex}00000{index2 + 1}", 'firstId': element['id'], 'title': subject_title}
            children.append(subject)
        element['children'] = children
    return content


def parse_chapter_fun():
    contents = subject_fun()
    with open("chapter_category.json", 'r', encoding='utf-8') as file:
        chapter_category_json = json.loads(file.read())
        for content in contents:
            for chapter_category in chapter_category_json:
                if content['title'] == chapter_category['first']:
                    for subject in content['children']:
                        if subject['title'] == chapter_category['title']:
                            chapter_category_result = []
                            for index, chapter_category_content in enumerate(chapter_category['content']):
                                chapter_category_result.append(
                                    handle_fun(chapter_category_content, index, subject['id'], content['title'],
                                               subject['title']))
                                pass
                            subject['children'] = chapter_category_result
    return contents


def handle_fun(chapter_category_content, index, subject_id, first, subject):
    chapter_category = {}
    global chapterIndex
    chapterIndex = chapterIndex + 1
    chapter_category['id'] = f"C{chapterIndex}00000{index + 1}"
    chapter_category['subjectId'] = subject_id
    chapter_category['title'] = chapter_category_content['title']
    categories = chapter_category_content['children']
    category_list = []
    for index, category in enumerate(categories):
        category_result = {}
        global categoryIndex
        categoryIndex = categoryIndex + 1
        category_result['id'] = f"D{categoryIndex + 1}00000{index + 1}"
        category_result['subjectId'] = chapter_category['subjectId']
        category_result['chapterId'] = chapter_category['id']
        category_result['title'] = category
        category_result['doSize'] = 0
        category_result['wrongCount'] = 0
        category_result['favoriteCount'] = 0
        category_result['children'] = question_data(category_result, first, subject,
                                                    chapter_category_content['title'], category)
        category_list.append(category_result)
        pass
    chapter_category['children'] = category_list
    return chapter_category


def question_data(category_result, first, subject, chapter, category):
    print(first + subject + chapter + category)
    with open("gongchengfagui.json", 'r', encoding='utf-8') as file:
        content2 = file.read()
        gcfg_json_data = json.loads(content2)
        result = []
        for index, elect in enumerate(gcfg_json_data):
            # print(elect['first'] + "-" + first + "=====" + elect['subject'] + "-" + subject + "=====" + elect['chapter'] + "-" + chapter + elect['category'] + "-" + category)
            if elect['first'] == first and elect['subject'] == subject and elect['chapter'] == chapter and elect['category'] == category:
                for index2, question in enumerate(elect['children']):
                    global questionIndex
                    questionIndex = questionIndex + 1
                    gcfg_json_result = {}
                    gcfg_json_result["continueCount"] = 0
                    gcfg_json_result["answerTime"] = 0
                    gcfg_json_result["myAnswer"] = ""
                    gcfg_json_result["title"] = question["title"]
                    gcfg_json_result["isDone"] = False
                    gcfg_json_result["categoryId"] = category_result['id']
                    gcfg_json_result["chapterId"] = category_result['chapterId']
                    gcfg_json_result["subjectId"] = category_result['subjectId']
                    gcfg_json_result["selectContentJson"] = question['selectContentJson']
                    gcfg_json_result["topicType"] = question["topicType"]
                    gcfg_json_result["isLookAnalysis"] = False
                    gcfg_json_result["imgPath"] = ""
                    gcfg_json_result["isShowWrongList"] = 0
                    gcfg_json_result["doCount"] = 0
                    gcfg_json_result["favoriteTime"] = 0
                    gcfg_json_result["id"] = f"Q{questionIndex}0000{index2 + 1}"
                    gcfg_json_result["correctAnswer"] = question["correctAnswer"]
                    gcfg_json_result["questionType"] = question["questionType"]
                    gcfg_json_result["errorCount"] = 0
                    gcfg_json_result["isFavorite"] = False
                    result.append(gcfg_json_result)
    return result


if __name__ == '__main__':
    # print(question_data(0, 0, 0, 0))
    print(json.dumps(parse_chapter_fun()))
